package edu.scau.divineplace.controller;

import edu.scau.divineplace.config.global.log.WebLog;
import edu.scau.divineplace.entity.query.CommentScoreQuery;
import edu.scau.divineplace.entity.query.JournalQuery;
import edu.scau.divineplace.entity.query.PageQuery;
import edu.scau.divineplace.entity.validate.group.commentscore.ContentAndUser;
import edu.scau.divineplace.entity.validate.group.commentscore.Id;
import edu.scau.divineplace.entity.vo.JournalVO;
import edu.scau.divineplace.service.JouanlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @Author: x1aolone
 * @Date: 2020/12/11 15:47
 */
@Slf4j
@RestController
@CrossOrigin
@Api("Journal控制器")
@WebLog("Journal控制器")
@Validated
@RequestMapping("/journal")
public class JournalController {

    @Autowired
    private JouanlService jouanlService;

    @ApiOperation("通过内容id获取内容的日志，支持完美分页")
    @GetMapping("/list/{contentId}")
    public List<JournalVO> list(PageQuery pageQuery, @PathVariable Long contentId){
        return jouanlService.list(pageQuery, contentId);
    }

    @ApiOperation("通过日志id获取该日志的信息")
    @GetMapping("/{journalId}")
    public JournalVO get(@PathVariable Long journalId){
        return jouanlService.get(journalId);
    }

    @ApiOperation("通过内容id、用户id获取用户发表的日志")
    @GetMapping("")
    public JournalVO get(@NotNull Long contentId, @NotNull Long userId){
        return jouanlService.get(contentId, userId);
    }

    @ApiOperation("通过内容类型获取该类型的内容的最近评论，支持完美分页")
    @GetMapping("/recent/{contentType}")
    public List<Map<String, Object>> listrRecentJournal(PageQuery pageQuery, @PathVariable Integer contentType) {
        return jouanlService.listRecentJournal(pageQuery, contentType);
    }

    @ApiOperation("新增评分")
    @PostMapping("/")
    public void add (@Validated(ContentAndUser.class) JournalQuery query) {
        jouanlService.add(query.getContentId(), query.getUserId(), query.getTitle(), query.getComment());
    }

    @ApiOperation("修改评分")
    @PutMapping("/")
    public void update (@Validated(ContentAndUser.class) JournalQuery query) {
        jouanlService.update(query.getContentId(), query.getUserId(), query.getTitle(), query.getComment());
    }

    @ApiOperation("修改评分")
    @PutMapping("/{journalId}")
    public void update (@PathVariable Long journalId, @Validated(Id.class) JournalQuery query) {
        jouanlService.update(journalId, query.getTitle(), query.getComment());
    }

}
